//
//  KLBookListController.m
//  KLBookStore
//
//  Created by apple on 2017/10/14.
//  Copyright © 2017年 NetEaseExam. All rights reserved.
//

#import "KLBookListController.h"

#import "KLBookListTableViewController.h"
#import "KLBookListCollectionViewController.h"

typedef NS_ENUM(NSUInteger, KLBookListMode){
    KLBookListModeTableView,
    KLBookListModeCollectionView
};

@interface KLBookListController ()

@property (nonatomic, assign) KLBookListMode listMode;

@end

@implementation KLBookListController

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
//    [self.childViewControllers makeObjectsPerformSelector:@selector(willMoveToParentViewController:) withObject:nil];
//    [self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//    [self.childViewControllers makeObjectsPerformSelector:@selector(removeFromParentViewController)];
//    [self.childViewControllers makeObjectsPerformSelector:@selector(didMoveToParentViewController:) withObject:nil];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNaviagtion];
    self.listMode = KLBookListModeTableView;
    [self switchToListMode:self.listMode];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initNaviagtion {
    self.navigationItem.title= @"我的藏书";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"list-switch-collection"] style:UIBarButtonItemStylePlain target:self action:@selector(didClickSwitchListButton:)];
}

- (void)didClickSwitchListButton:(UIBarButtonItem *)item {
    if (self.listMode == KLBookListModeTableView) {
        self.listMode = KLBookListModeCollectionView;
        item.image = [UIImage imageNamed:@"list-switch-table"];
    } else {
        self.listMode = KLBookListModeTableView;
        item.image = [UIImage imageNamed:@"list-switch-collection"];
    }
    [self switchToListMode:self.listMode];
}

- (void)switchToListMode:(KLBookListMode)mode {
    
    [self.childViewControllers makeObjectsPerformSelector:@selector(willMoveToParentViewController:) withObject:nil];
    [self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.childViewControllers makeObjectsPerformSelector:@selector(removeFromParentViewController)];
    [self.childViewControllers makeObjectsPerformSelector:@selector(didMoveToParentViewController:) withObject:nil];
    
    if (mode == KLBookListModeTableView) {
        KLBookListTableViewController *vc = [[KLBookListTableViewController alloc] init];
        [vc willMoveToParentViewController:self];
        [self addChildViewController:vc];
        [self.view addSubview:vc.view];
        vc.view.frame = self.view.bounds;
        [vc didMoveToParentViewController:self];
    } else {
        KLBookListCollectionViewController *vc = [[KLBookListCollectionViewController alloc] init];
        [vc willMoveToParentViewController:self];
        [self addChildViewController:vc];
        [self.view addSubview:vc.view];
        vc.view.frame = self.view.bounds;
        [vc didMoveToParentViewController:self];
    }
}














//
//    if (self.listMode == KLBookListModeTableView) {
//        self.listMode = KLBookListModeCollectionView;
//        KLBookListCollectionViewController *vc = [[KLBookListCollectionViewController alloc] init];
//        [vc willMoveToParentViewController:self];
//        [self addChildViewController:vc];
//        [self.view addSubview:vc.view];
//        vc.view.frame = self.view.bounds;
//        [vc didMoveToParentViewController:self];
////
////    } else {
//        self.listMode = KLBookListModeTableView;

//
//    }

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
